package com.bonorsoft.system.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bonorsoft.common.api.Result;
import com.bonorsoft.common.base.WilliamController;
import com.bonorsoft.system.entity.SysMoney;
import com.bonorsoft.system.service.SysMoneyService;
import com.bonorsoft.util.WxTokenAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WilliamYe
 * @since 2020-07-23
 */
@RestController
@RequestMapping("/money")
public class SysMoneyController extends WilliamController<SysMoney> {
    @Autowired
    private SysMoneyService moneyService;

    @GetMapping(value = "/list")
    public void list(){
        int count = moneyService.count();

        System.out.println(count);
    }

    /**
     * 根据登录凭证 code 获取 session_key 和 openid
     * @param code
     * @return
     */
    @GetMapping(value = "/auth")
    public Result<?> codeSession(@RequestParam(name="code",required=true) String code){
        JSONObject info = WxTokenAPI.getPeopleInfo(code);
        return info != null ? Result.ok(info) : Result.error("获取数据失败");
    }

    /**
     * 获取支付金额
     * @return
     */
    @GetMapping(value = "/getMoney")
    public Result<?> getMoney(){
        String money = moneyService.getMoney();
        return Result.ok(money);
    }

    /**
     * 获取缴费信息
     * @param money
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/querylist")
    public Result<?> queryPageList(SysMoney money,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<SysMoney> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pay_status","1").orderByAsc("sequence");
        Page<SysMoney> page = new Page<SysMoney>(pageNo, pageSize);
        IPage<SysMoney> pageList = moneyService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param money
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SysMoney money) {
        QueryWrapper<SysMoney> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pay_status","1").orderByAsc("sequence");
        List<SysMoney> list = moneyService.list(queryWrapper);
        return super.exportXls(list, request,money, SysMoney.class, "缴费信息");
    }

}
